#!/sbin/runscript
# ----------------------------------------------------------------------
# File:    /etc/init.d/zoneminder
# Purpose:  Startup the Zoneminder Server
# By:   Danky
# Date: 8-31-2010
# Upgrated By Christophe DAPREMONT (christophe_y2k@yahoo.fr) for zm >= V1.25.0 BUILD 3579 SVN on gentoo linux
# Date: 31 December 2011 Happy New Year
# ----------------------------------------------------------------------
opts="start stop status"
depend()   { 
      use mysql apache2
}
prog=ZoneMinder
ZM_PATH_BIN="/usr/bin"
command="$ZM_PATH_BIN/zmpkg.pl"
ZM_TMP1='mkdir /tmp/zm'
ZM_TMP2='chown -R apache:apache /tmp/zm'
ZM_RUN1='mkdir /var/run/zm'
ZM_RUN2='chown -R apache:apache /var/run/zm'
ZM_RM_TMP='rm -R /tmp/zm'
ZM_RM_RUN='rm -R /var/run/zm'

start() {
        ebegin "Create /tmp/zm directory"
        $ZM_TMP1
   eend $? "Failed to create /tmp/zm directory ! (probably already exist)"
   ebegin "chown -R apache:apache /tmp/zm"
        $ZM_TMP2
   eend $? "Failed chown command !"
   
   ebegin "Create /var/run/zm directory"
        $ZM_RUN1
   eend $? "Failed to create /var/run/zm directory ! (probably already exist)"
   ebegin "chown -R apache:apache /var/run/zm"
        $ZM_RUN2
   eend $? "Failed chown command !"
        ebegin "Starting Zoneminder"
        $command start
        eend $? "Failed to start $prog"
}

stop() {
        ebegin "Stopping $prog"
        $command stop
        eend $? "Failed to stop $prog"
        ebegin "Remove /var/run/zm directory"
        $ZM_RM_RUN
   eend $? "Failed to remove /var/run/zm directory !"
   ebegin "Remove /tmp/zm directory"
        $ZM_RM_TMP
   eend $? "Failed to remove /tmp/zm directory !"
}
status()   {
      result=`$command status`
      if [ "$result" = "running" ]; then
       echo "ZoneMinder is running"
       RETVAL=0 
      else
       echo "ZoneMinder is stopped"
       RETVAL=1
      fi
      exit ${RETVAL}
}